package com.zhdl.modules.demo.entity;

import cn.hutool.core.bean.BeanUtil;
import com.zhdl.hardware_contact.hardware_config.entity.ProjectConfiguration;
import com.zhdl.modules.process.ProcessStatusExchangeHolder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;


@Data
@Slf4j
public class MachineConf {
    private ProjectConfiguration manualModel = new ProjectConfiguration("手动模式","0",1,"60");
    private ProjectConfiguration autoModel = new ProjectConfiguration("自动模式","0",1,"60.1");
    private ProjectConfiguration fixModel = new ProjectConfiguration("检修模式","0",1,"60.2");
    private ProjectConfiguration startBtn = new ProjectConfiguration("启动按钮","0",1,"60.3");
    private ProjectConfiguration stopBtn = new ProjectConfiguration("停止按钮","0",1,"60.4");
    private ProjectConfiguration resetBtn = new ProjectConfiguration("复位按钮","0",1,"60.5");
    private ProjectConfiguration emStopBtn = new ProjectConfiguration("急停按钮","0",1,"60.6");
    private ProcessStatusExchangeHolder haveChildProcessService;
    public MachineConf(ProcessStatusExchangeHolder haveChildProcessService) {
        this.haveChildProcessService = haveChildProcessService;
    }

    public void checkValue(String address,String value){
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(this);
        for (String s : stringObjectMap.keySet()) {
            if (!s.equals("haveChildProcessService")){
                ProjectConfiguration projectConfiguration = (ProjectConfiguration) stringObjectMap.get(s);
                if (projectConfiguration.getAddress().equals(address)) {
                    if (!value.equals(projectConfiguration.getValue()) && value.equals("1")){
                        log.info("沿触发{}",projectConfiguration);
                        switch (address) {
                            case "60" -> haveChildProcessService.setAuto(false);
                            case "60.1" -> haveChildProcessService.setAuto(true);
                            case "60.3" -> haveChildProcessService.start();
                            case "60.4" -> haveChildProcessService.onPause();
                            case "60.5" -> haveChildProcessService.cancelAlarm();
                            case "60.6" -> haveChildProcessService.release();
                        }
                    }
                    projectConfiguration.setValue(value);
                }
            }

        }
    }
}
